home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / SPHERE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  6.3 KB  |  303 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* Don't take this program too seriously.  It is just a hack. */
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <math.h>
  14. #include <GL/glut.h>
  15.  
  16. GLfloat light_diffuse[] =
  17. {1.0, 0.0, 0.0, 1.0};
  18. GLfloat light_position[] =
  19. {1.0, 1.0, 1.0, 0.0};
  20. GLUquadricObj *qobj;
  21.  
  22. int win1, win2, submenu1, submenu2;
  23.  
  24. int list = 1;
  25.  
  26. float thetime = 0.0;
  27.  
  28. void
  29. display(void)
  30. {
  31.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  32.   if (glutGetWindow() == win1) {
  33.     glCallList(list);   /* render sphere display list */
  34.   } else {
  35.     glCallList(1);      /* render sphere display list */
  36.   }
  37.   glutSwapBuffers();
  38. }
  39.  
  40. void
  41. display_win1(void)
  42. {
  43.   glPushMatrix();
  44.   glTranslatef(0.0, 0.0, -1 - 2 * sin(thetime));
  45.   display();
  46.   glPopMatrix();
  47. }
  48.  
  49. void
  50. idle(void)
  51. {
  52.   GLfloat light_position[] =
  53.   {1.0, 1.0, 1.0, 0.0};
  54.  
  55.   glutSetWindow(win1);
  56.   thetime += 0.05;
  57.   light_position[1] = 1 + sin(thetime);
  58.   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  59.   display_win1();
  60. }
  61.  
  62. /* ARGSUSED */
  63. void
  64. delayed_stop(int value)
  65. {
  66.   glutIdleFunc(NULL);
  67. }
  68.  
  69. void
  70. it(int value)
  71. {
  72.   glutDestroyWindow(glutGetWindow());
  73.   printf("menu selection: win=%d, menu=%d\n", glutGetWindow(), glutGetMenu());
  74.   switch (value) {
  75.   case 1:
  76.     if (list == 1) {
  77.       list = 2;
  78.     } else {
  79.       list = 1;
  80.     }
  81.     break;
  82.   case 2:
  83.     exit(0);
  84.     break;
  85.   case 3:
  86.     glutAddMenuEntry("new entry", value + 9);
  87.     break;
  88.   case 4:
  89.     glutChangeToMenuEntry(1, "toggle it for drawing", 1);
  90.     glutChangeToMenuEntry(3, "motion done", 3);
  91.     glutIdleFunc(idle);
  92.     break;
  93.   case 5:
  94.     glutIdleFunc(NULL);
  95.     break;
  96.   case 6:
  97.     glutTimerFunc(2000, delayed_stop, 0);
  98.     break;
  99.   default:
  100.     printf("value = %d\n", value);
  101.   }
  102. }
  103.  
  104. void
  105. init(void)
  106. {
  107.   gluQuadricDrawStyle(qobj, GLU_FILL);
  108.   glNewList(1, GL_COMPILE);  /* create sphere display list */
  109.   gluSphere(qobj, /* radius */ 1.0, /* slices */ 20,  /* stacks 
  110.  
  111.                                                        */ 20);
  112.   glEndList();
  113.   gluQuadricDrawStyle(qobj, GLU_LINE);
  114.   glNewList(2, GL_COMPILE);  /* create sphere display list */
  115.   gluSphere(qobj, /* radius */ 1.0, /* slices */ 20,  /* stacks 
  116.  
  117.                                                        */ 20);
  118.   glEndList();
  119.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  120.   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  121.   glEnable(GL_LIGHTING);
  122.   glEnable(GL_LIGHT0);
  123.   glEnable(GL_DEPTH_TEST);
  124.   glMatrixMode(GL_PROJECTION);
  125.   gluPerspective( /* field of view in degree */ 40.0,
  126.   /* aspect ratio */ 1.0,
  127.     /* Z near */ 1.0, /* Z far */ 10.0);
  128.   glMatrixMode(GL_MODELVIEW);
  129.   gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
  130.     0.0, 0.0, 0.0,      /* center is at (0,0,0) */
  131.     0.0, 1.0, 0.);      /* up is in positive Y direction */
  132.   glTranslatef(0.0, 0.0, -1.0);
  133. }
  134.  
  135. void
  136. menustate(int inuse)
  137. {
  138.   printf("menu is %s\n", inuse ? "INUSE" : "not in use");
  139.   if (!inuse) {
  140.   }
  141. }
  142.  
  143. void
  144. keyboard(unsigned char key, int x, int y)
  145. {
  146.   if (isprint(key)) {
  147.     printf("key: `%c' %d,%d\n", key, x, y);
  148.   } else {
  149.     printf("key: 0x%x %d,%d\n", key, x, y);
  150.   }
  151. }
  152.  
  153. void
  154. special(int key, int x, int y)
  155. {
  156.   char *name;
  157.  
  158.   switch (key) {
  159.   case GLUT_KEY_F1:
  160.     name = "F1";
  161.     break;
  162.   case GLUT_KEY_F2:
  163.     name = "F2";
  164.     break;
  165.   case GLUT_KEY_F3:
  166.     name = "F3";
  167.     break;
  168.   case GLUT_KEY_F4:
  169.     name = "F4";
  170.     break;
  171.   case GLUT_KEY_F5:
  172.     name = "F5";
  173.     break;
  174.   case GLUT_KEY_F6:
  175.     name = "F6";
  176.     break;
  177.   case GLUT_KEY_F7:
  178.     name = "F7";
  179.     break;
  180.   case GLUT_KEY_F8:
  181.     name = "F8";
  182.     break;
  183.   case GLUT_KEY_F9:
  184.     name = "F9";
  185.     break;
  186.   case GLUT_KEY_F10:
  187.     name = "F11";
  188.     break;
  189.   case GLUT_KEY_F11:
  190.     name = "F12";
  191.     break;
  192.   case GLUT_KEY_LEFT:
  193.     name = "Left";
  194.     break;
  195.   case GLUT_KEY_UP:
  196.     name = "Up";
  197.     break;
  198.   case GLUT_KEY_RIGHT:
  199.     name = "Right";
  200.     break;
  201.   case GLUT_KEY_DOWN:
  202.     name = "Down";
  203.     break;
  204.   case GLUT_KEY_PAGE_UP:
  205.     name = "Page up";
  206.     break;
  207.   case GLUT_KEY_PAGE_DOWN:
  208.     name = "Page down";
  209.     break;
  210.   case GLUT_KEY_HOME:
  211.     name = "Home";
  212.     break;
  213.   case GLUT_KEY_END:
  214.     name = "End";
  215.     break;
  216.   case GLUT_KEY_INSERT:
  217.     name = "Insert";
  218.     break;
  219.   default:
  220.     name = "UNKONW";
  221.     break;
  222.   }
  223.   printf("special: %s %d,%d\n", name, x, y);
  224. }
  225.  
  226. void
  227. mouse(int button, int state, int x, int y)
  228. {
  229.   printf("button: %d %s %d,%d\n", button, state == GLUT_UP ? "UP" : "down", x, y);
  230. }
  231.  
  232. void
  233. motion(int x, int y)
  234. {
  235.   printf("motion: %d,%d\n", x, y);
  236. }
  237.  
  238. void
  239. visible(int status)
  240. {
  241.   printf("visible: %s\n", status == GLUT_VISIBLE ? "YES" : "no");
  242. }
  243.  
  244. void
  245. enter_leave(int state)
  246. {
  247.   printf("enter/leave %d = %s\n",
  248.     glutGetWindow(),
  249.     state == GLUT_LEFT ? "left" : "entered");
  250. }
  251.  
  252. int
  253. main(int argc, char **argv)
  254. {
  255.   qobj = gluNewQuadric();
  256.   glutInit(&argc, argv);
  257.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  258.   win1 = glutCreateWindow("sphere");
  259.   glutEntryFunc(enter_leave);
  260.   init();
  261.   glutDisplayFunc(display_win1);
  262.   glutCreateMenu(it);
  263.   glutAddMenuEntry("toggle draw mode", 1);
  264.   glutAddMenuEntry("exit", 2);
  265.   glutAddMenuEntry("new menu entry", 3);
  266.   glutAddMenuEntry("motion", 4);
  267.   glutAttachMenu(GLUT_LEFT_BUTTON);
  268.   glutCreateMenu(it);
  269.   glutAddMenuEntry("yes", 1);
  270.   glutAddMenuEntry("no", 2);
  271.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  272.   win2 = glutCreateWindow("second window");
  273.   glutEntryFunc(enter_leave);
  274.   glutKeyboardFunc(keyboard);
  275.   glutSpecialFunc(special);
  276.   glutMouseFunc(mouse);
  277. #if 0
  278.   glutMotionFunc(motion);
  279. #endif
  280.   glutVisibilityFunc(visible);
  281.   init();
  282.   light_diffuse[1] = 1;
  283.   light_diffuse[2] = 1;
  284.   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  285.   glutDisplayFunc(display);
  286.   submenu1 = glutCreateMenu(it);
  287.   glutAddMenuEntry("submenu a", 666);
  288.   glutAddMenuEntry("submenu b", 777);
  289.   submenu2 = glutCreateMenu(it);
  290.   glutAddMenuEntry("submenu 1", 25);
  291.   glutAddMenuEntry("submenu 2", 26);
  292.   glutAddSubMenu("submenuXXX", submenu1);
  293.   glutCreateMenu(it);
  294.   glutAddSubMenu("submenu", submenu2);
  295.   glutAddMenuEntry("stop motion", 5);
  296.   glutAddMenuEntry("delayed stop motion", 6);
  297.   glutAddSubMenu("submenu", submenu2);
  298.   glutAttachMenu(GLUT_LEFT_BUTTON);
  299.   glutMenuStateFunc(menustate);
  300.   glutMainLoop();
  301.   return 0;             /* ANSI C requires main to return int. */
  302. }
  303.